﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using AIClient.Players.Module.Card;
using AIClient.Utils;
using NetMsg.Card;
using NetMsg.CardAction;
using NetWorkMsg;

namespace AIClient.Players.Modules
{
    // 卡牌及卡牌战斗
    public class Card : Module
                      , IServerModule
    {
        public List<int>        Deck             { get; private set; } // 卡组
        public BitArray         Illustrated      { get; private set; } // 图鉴
        public int              GroupNum         { get; private set; } // 卡组数
        public TutorialProgress TutorialProgress { get; private set; } // 引导

        private BattleField _battleField;

        public Card(Player player) : base(player)
        {
        }


        public async ValueTask<Card> GetCardInfoAsync()
        {
            if (Deck == null)
                await Player.Gate.SendAsync(new GetCardInfoQ());

            return this;
        }

        #region 卡牌战斗相关

        private void CancelMatch()
        {
            Player.Gate.Send(new CancelMatchSignQ());
        }

        public void Surrender(int battleId)
        {
            Player.Gate.Send(new SurrenderQ{BattleId = battleId});
        }

        #endregion

        // 派发卡卡消息
        public void Dispatch(CardEnum tag, IUnPackable iUnPackable)
        {
            switch (tag)
            {
                case CardEnum.GetCardInfoP:
                    {
                        var msg = (GetCardInfoP) iUnPackable;
                        Deck             = msg.Deck;
                        Illustrated      = msg.Illustrated;
                        GroupNum         = msg.GroupNum;
                        TutorialProgress = msg.Progress;
                    }
                    break;
                case CardEnum.PutOnP:
                    {
                        var msg = (PutOnP) iUnPackable;
                        if (msg.Result != PutOnResult.Success)
                        {
                            Logger.LogError("AI放牌逻辑出错");
                            _battleField?.Surrender();
                        }
                    }
                    break;
            }
        }


        public void Dispatch(CardActionEnum tag, IUnPackable iUnPackable)
        {
            try
            {
                TryDispatch(tag, iUnPackable);
            }
            catch (Exception e)
            {
                _battleField?.Surrender();
                Logger.LogError(e.ToString());
            }
        }

        public void TryDispatch(CardActionEnum tag, IUnPackable iUnPackable)
        {
            switch (tag)
            {
                case CardActionEnum.ActionEnterSummaryP:
                    _battleField = new BattleField(this.Player, (ActionEnterSummaryP) iUnPackable);
                    break;
                case CardActionEnum.ActionEnterDetailP:
                    _battleField = new BattleField(this.Player, (ActionEnterDetailP) iUnPackable);
                    break;
                case CardActionEnum.ActionMatchOverP:
                    _battleField?.Clear();
                    _battleField = null;
                    break;
                default:
                    _battleField?.Dispatch(tag, iUnPackable);
                    break;
            }
        }

        public void DayRefresh()
        {
        }
    }
}